﻿using System;
using System.Collections;
using System.Collections.Generic;
using SCore.Util;
using UnityEngine.Networking;

namespace LocalizationUtils
{
    public static class HttpUtils
    {
        public static UnityWebRequest GetRequest(string url, int timeout = 15)
        {
            var www = UnityWebRequest.Get(url);
            www.timeout = timeout;
            www.downloadHandler = new DownloadHandlerBuffer();
            www.disposeDownloadHandlerOnDispose = true;
            return www;
        }

        public static void GetTextAsync(string url, Action<string, string> func, int timeout = 15)
        {
            var www = UnityWebRequest.Get(url);
            www.timeout = timeout;
            www.downloadHandler = new DownloadHandlerBuffer();
            www.disposeDownloadHandlerOnDispose = true;
            CoRoutineExecutors.Run(_GetTextAsync(www, func));
        }

        private static IEnumerator _GetTextAsync(UnityWebRequest www, Action<string, string> func)
        {
            using (www)
            {
                yield return www.SendWebRequest();
                if (www.isHttpError || www.isNetworkError)
                {
                    func(www.error, null);
                }
                else
                {
                    func(null, www.downloadHandler.text);
                }
            }
        }

        public static void GetBytesAsync(string url, Action<string, byte[]> func, int timeout = 15)
        {
            var www = UnityWebRequest.Get(url);
            www.timeout = timeout;
            www.downloadHandler = new DownloadHandlerBuffer();
            www.disposeDownloadHandlerOnDispose = true;
            CoRoutineExecutors.Run(_GetBytesAsync(www, func));
        }

        public static void GetBytesAsync(string url, Action<string, byte[]> func, Dictionary<string, string> headers,
            int timeout = 15)
        {
            var www = UnityWebRequest.Get(url);
            foreach (var header in headers)
            {
                www.SetRequestHeader(header.Key, header.Value);
            }

            www.timeout = timeout;
            www.downloadHandler = new DownloadHandlerBuffer();
            www.disposeDownloadHandlerOnDispose = true;
            CoRoutineExecutors.Run(_GetBytesAsync(www, func));
        }

        private static IEnumerator _GetBytesAsync(UnityWebRequest www, Action<string, byte[]> func)
        {
            using (www)
            {
                yield return www.SendWebRequest();
                if (www.isHttpError || www.isNetworkError)
                {
                    func(www.error, null);
                }
                else
                {
                    func(null, www.downloadHandler.data);
                }
            }
        }

        public static String TimestampUrl(string url)
        {
            var timestamp = GetTimestamp();
            url = url + string.Format("?v={0}&t={0}", timestamp);
            return url;
        }

        public static int GetUnixTime()
        {
            var epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            return (int)epoch;
        }

        public static int GetTimestamp()
        {
            return GetUnixTime();
        }
    }
}